######## Load Packages
pacman::p_load(readxl,
               tidyverse,
               readstata13,
               data.table,
               writexl,
               RStata,
               AER,
               lfe,
               concordance,
               stargazer,
               imputeTS,
               zoo)
########

######## set dir
setwd("C:\\Users\\Sean\\Documents\\Pres")
########

names <- c("county_name", "state_abbrev",	"Total Vote",	"dem_vote",	"rep_vote",	"other_vote","Margin",	"Margin (%)", "dem_perc_vote", "rep_perc_vote", "third_perc_vote",
           "other_perc_vote", "dem_raw_vote", "rep_raw_vote", "other_raw_vote", "county_name_2",	"state_abbrev_2",	"ST",	"CTY",	"FIPS",	"LSAD_TRANS", "Year")

pres_1972 <- read_xls("Pres_Election_Data_1972.xls", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,42,43,46,47,48,50) %>% mutate(Year = 1972)
colnames(pres_1972) <- names

pres_1976 <- read_xls("Pres_Election_Data_1976.xls", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,42,43,46,47,48,50) %>% mutate(Year = 1976)
colnames(pres_1976) <- names

pres_1980 <- read_xlsx("Pres_Election_Data_1980.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,47,48,51,52,53,55) %>% mutate(Year = 1980)
colnames(pres_1980) <- names

pres_1984 <- read_xlsx("Pres_Election_Data_1984.xlsx", sheet = 4) %>% select(1:8,10,11,12,13,14,15,16,42,43,46,47,48,50) %>% mutate(Year = 1984)
colnames(pres_1984) <- names

pres_1988 <- read_xlsx("Pres_Election_Data_1988.xlsx", sheet = 5) %>% select(1:8,10,11,12,13,14,15,16,47,48,51,52,53,55) %>% mutate(Year = 1988)
colnames(pres_1988) <- names

pres_1992 <- read_xlsx("Pres_Election_Data_1992.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,49,50,53,54,55,57) %>% mutate(Year = 1992)
colnames(pres_1992) <- names

pres_1996 <- read_xlsx("Pres_Election_Data_1996.xlsx", sheet = 4) %>% select(1:8,10,11,12,13,14,15,16,47,48,51,52,53,55) %>% mutate(Year = 1996)
colnames(pres_1996) <- names

pres_2000 <- read_xlsx("Pres_Election_Data_2000.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,42,43,46,47,48,50) %>% mutate(Year = 2000)
colnames(pres_2000) <- names

pres_2004 <- read_xlsx("Pres_Election_Data_2004.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,42,43,46,47,48,50) %>% mutate(Year = 2004)
colnames(pres_2004) <- names

pres_2008 <- read_xlsx("Pres_Election_Data_2008.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,49,50,53,54,55,57) %>% mutate(Year = 2008)
colnames(pres_2008) <- names

pres_2012 <- read_xlsx("Pres_Election_Data_2012.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,54,55,58,59,60,62) %>% mutate(Year = 2012)
colnames(pres_2012) <- names

pres_2016 <- read_xlsx("Pres_Election_Data_2016.xlsx", sheet = 3) %>% select(1:8,10,11,12,13,14,15,16,58,59,62,63,64,66) %>% mutate(Year = 2016)
colnames(pres_2016) <- names

pres <- rbind(pres_1972, pres_1976, pres_1980, pres_1984, pres_1988, pres_1992, pres_1996, pres_2000, pres_2004, pres_2008, pres_2012, pres_2016) %>% unique()

write_xlsx(pres, "presidential_data.xlsx")
